var queues = require('mysql-queues');
const DEBUG = true;
var select = function(connection, callback) {
	var sql = 'select * from record';
	connection.query(sql, function(err, rows) {
		if (err){
			console.log(err);
			console.log(err.stack);
		}
		callback(rows);
		connection.release();
	});
},
	add = function(connection, data, callback) {
		queues(connection, DEBUG);
		var sql = 'insert into record(content,flag,employeeId,effectiveDate) values(?,?,?,?)';
		var trans = connection.startTransaction();
		trans.query(sql, [ data.content,data.flag, data.employeeId,data.effectiveDate], function(err, rows) {
			if (err) {
				console.log(err);
				console.log(err.stack);
				trans.rollback();
			} else {
				trans.commit(function(err, info) {
					console.log(info);
				});
			}
			callback(rows);
		});
		trans.execute();
		console.log('execute addUserRole');
		connection.release();
	},
	delet = function(connection, id, callback) {
		queues(connection, DEBUG);
		var sql = 'delete from record where id=?';
		var trans = connection.startTransaction();
		trans.query(sql, id, function(err, rows) {
			if (err) {
				console.log(err);
				console.log(err.stack);
				trans.rollback();
			} else {
				trans.commit(function(err, info) {
					console.log(info);
				});
			}
			console.log('rows', rows);
			callback(rows);
		});
		trans.execute();
		console.log('execute');
		connection.release();
	},
	update = function(connection, data, callback) {
		queues(connection, DEBUG);
		var sql = 'update user_role set recordName=?,type=?,content=?,flag=?,employeeId=?,recordDate=? where id=?';
		var trans = connection.startTransaction();
		trans.query(sql, [data.recordName, data.type, data, content, data.flag, data.employeeId, data.recordDate, data.id], function(err, rows) {
			if (err) {
				console.log(err);
				console.log(err.stack);
				trans.rollback();
			} else {
				trans.commit(function(err, info) {
					console.log('info:', info);
				});
			}
			console.log('rows', rows);
			callback(rows);
		});
		trans.execute();
		console.log('execute update');
		connection.release();
	},
	findById = function(connection, id, callback) {
		var sql = 'select * from record where id=?';
		connection.query(sql, id, function(err, rows) {
			callback(rows);
			connection.release();
		});
	},
	findByEmployeeId = function(connection, id, callback) {
		var sql = 'select * from record where employeeId=?';
		connection.query(sql, id, function(err, rows) {
			if (err) {
				console.log(err);
				console.log(err.stack);
				callback(err);
			}
			callback(rows);
			connection.release();
		});
	},
	findByKeyAndId = function(connection, data, callback) {
		var sql = 'select * from record where (flag =? or content like ? ) and (employeeId=?)';
		console.info(data.key);
		var flag;
		if(data.key=="职位调整"){
			flag=1;
		}else if(data.key=="薪资调整"){
			flag=2;
		}else if(data.key=="部门调整"){
			flag=3;
		}else if(data.key=="合同调整"){
			flag=4;
		}
		connection.query(sql, [flag, '%'+data.key+'%', data.id], function(err, rows) {
			if (err) {
				console.log(err);
				console.log(err.stack);
				callback(err);
			}
			callback(rows);
			connection.release();
		});
	}

module.exports = {
	selectRecord: select,
	addRecord: add,
	deleteRecord: delet,
	updateRecord: update,
	findById: findById,
	findByEmployeeId: findByEmployeeId,
	findByKeyAndId: findByKeyAndId
};